local chidan = fk.CreateSkill {
  name = "inker_chidan",
  tags = { Skill.Family, Skill.Spirited },
  max_game_use_time = 1,
}

local DIY = require "packages.diy_utility.diy_utility"
local IU = require "packages.inker.pkg.util.inker_util"

chidan:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(chidan.name) then return end
    local room = player.room
    local cards = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local c = Fk:getCardById(info.cardId)
          if c.color == Card.Red and room:getCardArea(c) == Card.DiscardPile and DIY.getCardLastOwner(room, c) == player then
            table.insert(cards, c)
          end
        end
      end
    end

    if #cards > 0 then
      event:setSkillData(self, "inker_chidan_cards", cards)
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local family_member = table.filter(room:getAlivePlayers(false), function(p)
      return IU.FamilyMember(player, p)
    end)
    local cards = {}
    for _, card in ipairs(event:getSkillData(self, "inker_chidan_cards")) do
      if card:isCommonTrick() or card.type == Card.TypeBasic then
        table.insertIfNeed(cards, card.name)
      end
    end
    if #cards == 0 then return end
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "#inker_chidan_active",
      prompt = "#inker_chidan-invoke",
      extra_data = {
        skillName = chidan.name,
        vs_names = cards,
        targets = family_member,
      }
    })
    if dat then
      event:setCostData(self, { interaction = dat.interaction, cards = dat.cards, tos = dat.targets })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    room:askToUseVirtualCard(dat.tos[1], {
      name = dat.interaction,
      subcards = dat.cards,
      skill_name = chidan.name,
      prompt = "#inker_chidan-use:::" .. Fk:getCardById(dat.cards[1]):toLogString() .. ":" .. dat.interaction,
    })
  end
}, { check_skill_limit = true })
chidan:addEffect(fk.AfterCardsMove, { --激昂
  is_delay_effect = true,
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    if chidan:withinTimesLimit(player, Player.HistoryGame) then return false end
    for _, move in ipairs(data) do
      local room = player.room
      if move.toArea == Card.DiscardPile and not table.contains(player:getTableMark("@inker_chidan_method"), Util.moveReasonMapper(move.moveReason)) then
        for _, info in ipairs(move.moveInfo) do
          local c = Fk:getCardById(info.cardId)
          if c.trueName == "slash" and room:getCardArea(c) == Card.DiscardPile and DIY.getCardLastOwner(room, c) == player then
            room:addTableMark(player, "@inker_chidan_method", Util.moveReasonMapper(move.moveReason))
          end
        end
      end
    end

    return #player:getTableMark("@inker_chidan_method") >= 2
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@inker_chidan_method", 0)
    player:clearSkillHistory(chidan.name)
  end
})

Fk:loadTranslationTable{
  ["inker_chidan"] = "赤胆",
  [":inker_chidan"] = "宗族技，昂扬技，当你的红色牌进入弃牌堆后，你可以将【杀】当其中一张即时牌令同族角色使用。<br>" ..
  "激昂：你以两种方式失去【杀】。",

  ["#inker_chidan-invoke"] = "赤胆：你可以选择一张【杀】和其中一张即时牌，令同族角色将此【杀】当即时牌使用",
  ["#inker_chidan-use"] = "赤胆：请将 %arg 当【%arg2】使用",

  ["@inker_chidan_method"] = "赤胆",

  ["$inker_chidan1"] = "挥刀取寇首，千里单骑走！",
  ["$inker_chidan2"] = "金刀烈马，亦能助军威！",
}

return chidan